<?php

namespace app\system\controller;

use think\Controller;
use think\Db;
use think\Request;

class Component extends Controller
{
    public function uploader(){
        if(request()->file('editormd-image-file')){
            $file =request()->file('editormd-image-file');
        }else{
            $file = request()->file('image');
        }
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move( 'uploads');
        if($info){
            $imginfo=[
                'name'=>$file->getInfo('name'),
                'filename'=>$info->getFilename(),
                'path'=>'/uploads/'.$info->getSaveName(),
                'extension'=>$info->getExtension(),
                'createtime'=>time(),
                'size'=>$file->getInfo('size')
            ];
            $imgSave=Db::name('attachment')->insert($imginfo);
            if($imgSave){
                $domain=\request()->domain();
                $res=[
                    'success'=>1,
                    'message'=>'上传成功',
                    'code'=>0,
                    'msg'=>'上传成功',
                    'data'=>[
                        'src'=>$domain.'/uploads/'.$info->getSaveName()
                    ],
                    'url'=>$domain.'/uploads/'.$info->getSaveName()
                ];
                return json($res);
            }

        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
}
